package at.ac.tuwien.lsds.csv;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;

import at.ac.tuwien.lsds.model.Application;
import at.ac.tuwien.lsds.model.ApplicationImpl;
import at.ac.tuwien.lsds.util.Settings;

public class ReaderImpl implements Reader {

	private static final Logger log = Logger.getLogger(ReaderImpl.class);

	private FileReader fr;
	private String separator = ";";

	@Override
	public List<Application> readInputData() throws NumberFormatException, IOException {
		List<Application> appList = new ArrayList<Application>();
		String[] splitArray = null;
		String currentLine;

		this.fr = new FileReader(Settings.getInputFilePath());
		BufferedReader data = new BufferedReader(fr);

		boolean firstLine = true;
		Integer timestamp, hdd, ram, cpu, duration;
		while ((currentLine = data.readLine()) != null) {
			if(firstLine) {
				firstLine = false;
				continue;
			}
			splitArray = currentLine.split(separator);
			int i = 0;
			timestamp = Integer.parseInt(splitArray[i++].trim());
			hdd = Integer.parseInt(splitArray[i++].trim());
			ram = Integer.parseInt(splitArray[i++].trim());
			cpu = Integer.parseInt(splitArray[i++].trim());
			duration = Integer.parseInt(splitArray[i++].trim());
			appList.add(new ApplicationImpl(hdd, ram, cpu, timestamp, duration));
		}
		log.info("Read " + appList.size() + " Applications!");
		return appList;
	}
	
}
